package com.lanchetech.user.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanchetech.bean.request.ActivityBindSpuReq;
import com.lanchetech.bean.request.OrderReq;
import com.lanchetech.bean.response.BasePageResp;
import com.lanchetech.bean.response.ResultData;
import com.lanchetech.bean.response.SpuResp;
import com.lanchetech.bean.util.PageHelpUtil;
import com.lanchetech.bean.vo.SkuItemVO;
import com.lanchetech.dao.ActivityBindSpuMapper;
import com.lanchetech.dao.ActivityMapper;
import com.lanchetech.dao.SkuMapper;
import com.lanchetech.dao.SpuMapper;
import com.lanchetech.entity.*;
import com.lanchetech.user.config.ReturnPayInfoVO;
import com.lanchetech.user.service.ActivityService;
import com.lanchetech.user.service.OrderService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ActivityServiceImpl implements ActivityService {

    @Resource
    private OrderService orderService;
    @Resource
    ActivityBindSpuMapper bindSpuMapper;
    @Resource
    private SpuMapper spuMapper;
    @Resource
    private SkuMapper skuMapper;
    @Resource
    private ActivityMapper activityMapper;

    @Override
    public ResultData<ReturnPayInfoVO> createNewUserPayOrder(User user, OrderReq req, HttpServletRequest httpServletRequest) {
        ResultData<ReturnPayInfoVO> wechatPayOrder = orderService.createWechatPayOrder(user, req, httpServletRequest);
        return wechatPayOrder;
    }


    @Override
    public ResultData<BasePageResp<SpuResp>> getBindPage(ActivityBindSpuReq req) {
        Page page = PageHelper.startPage(req.getPageNo(), req.getPageSize());
        List<SpuResp> respList = new ArrayList<>();
        ActivityBindSpu bindSpu = new ActivityBindSpu();
        bindSpu.setActivityId(req.getActivityId());
        List<ActivityBindSpu> list = bindSpuMapper.getList(bindSpu);
        if (CollectionUtils.isNotEmpty(list)) {
            // 处理spuIds
            List<Long> spuIds = list.stream().map(ActivityBindSpu::getSpuId).distinct().collect(Collectors.toList());
            List<Spu> spuList = spuMapper.findAllByIds(spuIds);

            // 活动信息
            Map<Long, Long> collect = list.stream().collect(Collectors.toMap(ActivityBindSpu::getSpuId, ActivityBindSpu::getActivityId, (t1, t2) -> t1));
            List<Activity> activities = activityMapper.selectBatchIds(list.stream().map(ActivityBindSpu::getActivityId).distinct().collect(Collectors.toList()));
            Map<Long, Activity> activityMap = activities.stream().collect(Collectors.toMap(Activity::getId, s -> s, (t1, t2) -> t1));
            spuList.forEach(s -> {
                SpuResp resp = new SpuResp();
                BeanUtils.copyProperties(s, resp);
                Long activityId = collect.get(resp.getId());
                Activity activity = activityMap.get(activityId);
                resp.setActivityId(activityId);
                resp.setActivityType(activity.getType());
                respList.add(resp);
            });
            // 新人专区活动价格
            Map<Long, BigDecimal> newPriceMap = list.stream().collect(Collectors.toMap(ActivityBindSpu::getSkuId, ActivityBindSpu::getNewPrice, (t1, t2) -> t1));
            List<Long> skuIds = list.stream().map(ActivityBindSpu::getSkuId).collect(Collectors.toList());
            List<Sku> skuList = skuMapper.findAllByIds(skuIds);
            List<SkuItemVO> skuItemVOList = new ArrayList<>();
            for (Sku sku : skuList) {
                SkuItemVO itemVO = new SkuItemVO();
                BeanUtils.copyProperties(sku, itemVO);
                itemVO.setNewPrice(newPriceMap.get(itemVO.getId()));
                skuItemVOList.add(itemVO);
            }
            Map<Long, List<SkuItemVO>> skuMap = skuItemVOList.stream().collect(Collectors.groupingBy(SkuItemVO::getSpuId));
            respList.forEach(t -> t.setSkuItemVOS(skuMap.get(t.getId())));
        }
        return PageHelpUtil.buildPage(respList, page);
    }
}
